package com.kanshu.mvp_pro.mvp;

import android.os.Bundle;
import androidx.annotation.IdRes;

import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public abstract class BaseFragmentDelegate implements IFragmentDelegate {

    private View mRootView;

    private final SparseArray<View> mViews = new SparseArray<>();



    @Override
    public void create(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        int rootLayoutId = provideLayoutRes();
        mRootView = inflater.inflate(rootLayoutId, container, false);
    }

    public View bindView(@IdRes int id) {
        View view = mViews.get(id);
        if (view == null) {
            view = getRootView().findViewById(id);
            mViews.put(id, view);
        }

        return view;
    }

    public View getViewById(@IdRes int id) {
        return bindView(id);
    }

    @Override
    public View getRootView() {
        return mRootView;
    }

    @Override
    public void initWidget() {

    }

    public void onClickedListener(View.OnClickListener listener, @IdRes int... ids) {
        if (ids == null) {
            return;
        }

        for (int id : ids) {
            getViewById(id).setOnClickListener(listener);
        }

    }

}
